package cn.com.customer.manager.ui.task.addTask

import cn.com.customer.manager.bean.*
import cn.com.customer.manager.http.BaseApi
import cn.com.customer.manager.http.subscriber.NoTipRequestSubscriber
import cn.com.customer.manager.http.subscriber.TipRequestSubscriber
import cn.com.customer.manager.manager.TokenManager
import com.xuexiang.xhttp2.XHttp
import com.xuexiang.xhttp2.XHttpProxy
import com.xuexiang.xhttp2.annotation.ThreadType
import com.xuexiang.xhttp2.model.ApiResult

class AddTaskPresenter constructor(view: AddTaslContract.View) : AddTaslContract.Presenter {

    private var mView: AddTaslContract.View = view
    val request = XHttp.custom()!!
    init {
       start()
    }
    override fun addTask(params: TaskAddInfo) {
        request.call(request.create(BaseApi.ITask::class.java)
                .taskAdd(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<Map<String, String>?>() {
                    override fun onSuccess(apiResult: Map<String, String>?) {
                        apiResult?.let {
                            mView.addSuccess()
                        }
                    }
                })
    }

    override fun start() {
        XHttpProxy.proxy(BaseApi.ITask::class.java, ThreadType.TO_MAIN)
                .taskRemindWayList(TokenManager.getInstance().token)
                .subscribeWith(object : NoTipRequestSubscriber<RemindWayResult>() {
                    override fun onSuccess(t: RemindWayResult?) {
                        t?.let {
                        mView.setMarkType(it)
                        }
                    }

                })

        XHttpProxy.proxy(BaseApi.ITask::class.java, ThreadType.TO_MAIN)
                .taskTypeList(TokenManager.getInstance().token)
                .subscribeWith(object : NoTipRequestSubscriber<TypeResult>() {
                    override fun onSuccess(t: TypeResult?) {
                        t?.let {
                            mView.setTaskType(t)
                        }
                    }
                })
        var parameters = mutableMapOf<String,String>()
        parameters["pageNo"] = "1"
        parameters["pageSize"] = "10000"
        request.call(request.create(BaseApi.IAuthorization::class.java)
                .userList(RequestBean(parameters)))
                .subscribeWith(object : TipRequestSubscriber<ApiResult<List<UserList>>?>() {
                    override fun onSuccess(apiResult: ApiResult<List<UserList>>?) {
                        apiResult?.let {
                         mView.setUser(apiResult.data as MutableList<UserList>)
                        }
                    }
                })
    }


}